package com.example.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.jeasy.rules.core.RuleBuilder;
import org.jeasy.rules.mvel.MVELRuleFactory;
import org.jeasy.rules.spel.SpELRuleFactory;
import org.jeasy.rules.support.RuleDefinition;
import org.jeasy.rules.support.reader.JsonRuleDefinitionReader;
import org.jeasy.rules.support.reader.RuleDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.expression.BeanResolver;
import org.springframework.expression.ParserContext;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test/easyRule")
public class Controller {
    /**
     * 启动规则:
     */
    @RequestMapping("/fireRule")
    public void fireRule() {
        Facts facts = new Facts();
        facts.put("rain", true);
        Rule ruleA = new RuleBuilder()
                .name("规则A")
                .priority(1)
                .description("规则A的描述")
                .when(facts_ -> facts_.get("rain").equals(true))
                .then(facts_ -> System.err.println("如果规则A中条件满足===》天在下雨，待在家里"))
                .build();
        Rule ruleB = new RuleBuilder()
                .name("规则B")
                .priority(2)
                .description("规则B的描述")
                .when(facts_ -> facts_.get("rain").equals(false))
                .then(facts_ -> System.err.println("如果规则B中条件满足===》天不在下雨，可以去玩了"))
                .build();
        Rules rules = new Rules();
        rules.register(ruleA);
        rules.register(ruleB);
        RulesEngine rulesEngine = new DefaultRulesEngine();
        rulesEngine.fire(rules, facts);
    }

    /**
     * 启动规则-jsonRule
     */
    @PostMapping("/fireRule/mvel")
    public void fireRuleForMvel(@RequestBody JSONArray ruleJson) {
        RuleDefinitionReader jsonRuleDefinitionReader = new JsonRuleDefinitionReader();
        // 构建rules- json
        MVELRuleFactory mvelRuleFactoryFromJson = new MVELRuleFactory(jsonRuleDefinitionReader);
        Rules rulesFromJsonFile = null;
        try {
            rulesFromJsonFile = mvelRuleFactoryFromJson.createRules(new StringReader(ruleJson.toJSONString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Facts facts = new Facts();
        facts.put("rain", true);
        RulesEngine rulesEngine = new DefaultRulesEngine();
        rulesEngine.fire(rulesFromJsonFile, facts);
    }

    /**
     * 启动规则-spring spel
     */
    @PostMapping("/fireRule/spel")
    public void fireRuleForSpel(@RequestBody JSONArray ruleJson) {
        RuleDefinitionReader jsonRuleDefinitionReader = new JsonRuleDefinitionReader();
        // 构建rules- json
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MySpringAppConfig.class);
        BeanResolver beanResolver = new MySpringBeanResolver(applicationContext);
        SpELRuleFactory spelRuleFactoryFromJson = new SpELRuleFactory(jsonRuleDefinitionReader, ParserContext.TEMPLATE_EXPRESSION, beanResolver);
        Rules rulesFromJsonFile = null;
        try {
            rulesFromJsonFile = spelRuleFactoryFromJson.createRules(new StringReader(ruleJson.toJSONString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Facts facts = new Facts();
        RulesEngine rulesEngine = new DefaultRulesEngine();
        rulesEngine.fire(rulesFromJsonFile, facts);
    }

    /**
     * 考勤规则
     */
    @PostMapping("/fireRule/demo")
    public void fireRuleDemo(@RequestBody JSONObject ruleJson) {
        RuleDefinitionReader jsonRuleDefinitionReader = new JsonRuleDefinitionReader();
        // 构建rules- json
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MySpringAppConfig.class);
        BeanResolver beanResolver = new MySpringBeanResolver(applicationContext);
        SpELRuleFactory spelRuleFactoryFromJson = new SpELRuleFactory(jsonRuleDefinitionReader, ParserContext.TEMPLATE_EXPRESSION, beanResolver);
        Rules rulesFromJsonFile = null;
        try {
            rulesFromJsonFile = spelRuleFactoryFromJson.createRules(new StringReader(ruleJson.getJSONArray("ruleJson").toJSONString()));
        } catch (Exception e) {
            e.printStackTrace();
        }


        JSONArray personData = ruleJson.getJSONArray("data");
        for (int i = 0; i < personData.size(); i++) {
            Facts facts = new Facts();
            Person person = JSONObject.toJavaObject(personData.getJSONObject(i), Person.class);
            facts.put("person", person);
            RulesEngine rulesEngine = new DefaultRulesEngine();
            rulesEngine.fire(rulesFromJsonFile, facts);
        }
    }
}
